package com.bear.atlassianmcp.oauth.model;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;

import java.time.LocalDateTime;

/**
 * OAuth Token 数据模型
 * 用于存储 OAuth 1.0a 的 Token 信息
 */
@JsonIgnoreProperties(ignoreUnknown = true)
public class OAuthToken {
    
    /**
     * Token 值
     */
    @JsonProperty("token")
    private String token;
    
    /**
     * Token Secret
     */
    @JsonProperty("tokenSecret")
    private String tokenSecret;
    
    /**
     * Token 类型 (REQUEST_TOKEN, ACCESS_TOKEN)
     */
    @JsonProperty("tokenType")
    private TokenType tokenType;
    
    /**
     * 授权验证码 (仅用于 Request Token)
     */
    @JsonProperty("verifier")
    private String verifier;
    
    /**
     * Token 创建时间
     */
    @JsonProperty("createdAt")
    private LocalDateTime createdAt;
    
    /**
     * Token 过期时间 (可选)
     */
    @JsonProperty("expiresAt")
    private LocalDateTime expiresAt;
    
    /**
     * 关联的用户标识
     */
    @JsonProperty("userId")
    private String userId;
    
    /**
     * 服务类型 (JIRA, CONFLUENCE)
     */
    @JsonProperty("serviceType")
    private ServiceType serviceType;
    
    /**
     * 是否已授权确认
     */
    @JsonProperty("authorized")
    private boolean authorized = false;
    
    // 构造函数
    public OAuthToken() {
        this.createdAt = LocalDateTime.now();
    }
    
    public OAuthToken(String token, String tokenSecret, TokenType tokenType, ServiceType serviceType) {
        this();
        this.token = token;
        this.tokenSecret = tokenSecret;
        this.tokenType = tokenType;
        this.serviceType = serviceType;
    }
    
    // Getters and Setters
    public String getToken() {
        return token;
    }
    
    public void setToken(String token) {
        this.token = token;
    }
    
    public String getTokenSecret() {
        return tokenSecret;
    }
    
    public void setTokenSecret(String tokenSecret) {
        this.tokenSecret = tokenSecret;
    }
    
    public TokenType getTokenType() {
        return tokenType;
    }
    
    public void setTokenType(TokenType tokenType) {
        this.tokenType = tokenType;
    }
    
    public String getVerifier() {
        return verifier;
    }
    
    public void setVerifier(String verifier) {
        this.verifier = verifier;
    }
    
    public LocalDateTime getCreatedAt() {
        return createdAt;
    }
    
    public void setCreatedAt(LocalDateTime createdAt) {
        this.createdAt = createdAt;
    }
    
    public LocalDateTime getExpiresAt() {
        return expiresAt;
    }
    
    public void setExpiresAt(LocalDateTime expiresAt) {
        this.expiresAt = expiresAt;
    }
    
    public String getUserId() {
        return userId;
    }
    
    public void setUserId(String userId) {
        this.userId = userId;
    }
    
    public ServiceType getServiceType() {
        return serviceType;
    }
    
    public void setServiceType(ServiceType serviceType) {
        this.serviceType = serviceType;
    }
    
    public boolean isAuthorized() {
        return authorized;
    }
    
    public void setAuthorized(boolean authorized) {
        this.authorized = authorized;
    }
    
    /**
     * 检查 Token 是否过期
     */
    public boolean isExpired() {
        if (expiresAt == null) {
            return false; // 没有设置过期时间，认为不过期
        }
        return LocalDateTime.now().isAfter(expiresAt);
    }
    
    /**
     * 检查是否为有效的 Access Token
     */
    public boolean isValidAccessToken() {
        return tokenType == TokenType.ACCESS_TOKEN 
               && token != null 
               && tokenSecret != null 
               && authorized 
               && !isExpired();
    }
    
    /**
     * 检查是否为有效的 Request Token
     */
    public boolean isValidRequestToken() {
        return tokenType == TokenType.REQUEST_TOKEN 
               && token != null 
               && tokenSecret != null 
               && !isExpired();
    }
    
    @Override
    public String toString() {
        return "OAuthToken{" +
               "token='" + (token != null ? token.substring(0, Math.min(token.length(), 8)) + "..." : "null") + '\'' +
               ", tokenType=" + tokenType +
               ", serviceType=" + serviceType +
               ", authorized=" + authorized +
               ", createdAt=" + createdAt +
               ", expiresAt=" + expiresAt +
               '}';
    }
    
    /**
     * Token 类型枚举
     */
    public enum TokenType {
        /**
         * 请求 Token
         */
        REQUEST_TOKEN,
        
        /**
         * 访问 Token
         */
        ACCESS_TOKEN
    }
    
    /**
     * 服务类型枚举
     */
    public enum ServiceType {
        /**
         * Jira 服务
         */
        JIRA,
        
        /**
         * Confluence 服务
         */
        CONFLUENCE
    }
}
